#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QString>
#include <agespinboxdelegate.h>
#include <addrdelegate.h>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , ad(new AddDialog)
//    ,ageDelegate(new AgeSpinBoxDelegate)
{
    ui->setupUi(this);

    tbv = ui->tableView;

    QStringList heads;
    heads << "学号" << "姓名" << "年龄" << "地址";

    QStandardItemModel *model = new QStandardItemModel(this);

    model->setHorizontalHeaderLabels(heads);

    ad->setModel(model);
    ad->setStu(Student());

    initModel1(model);
    tbv->setModel(model);

//    tbv->setItemDelegate(ageDelegate);
    AgeSpinBoxDelegate *ageDelegate = new AgeSpinBoxDelegate;
    tbv->setItemDelegateForColumn(2,ageDelegate);

    AddrDelegate *delegate2 = new AddrDelegate;
    tbv->setItemDelegateForColumn(3,delegate2);

    // 设置单选、多选或选择列
    tbv->setSelectionMode(QAbstractItemView::MultiSelection);
//    tbv->setSelectionMode(QAbstractItemView::ExtendedSelection);
//    tbv->setSelectionMode(QAbstractItemView::SingleSelection);

    // 设置选择行为，选全行，选选多个列，
    tbv->setSelectionBehavior(QAbstractItemView::SelectRows);
//    tbv->setSelectionBehavior(QAbstractItemView::SelectItems);
//    tbv->setSelectionBehavior(QAbstractItemView::SelectColumns);

    tbv->setContextMenuPolicy(Qt::CustomContextMenu);


    rightMenu = new QMenu(tbv);
    cutAction = new QAction("剪切",tbv);
    copyAction = new QAction("复制",tbv);
    pasteAction = new QAction("粘贴",tbv);
    deleteAction = new QAction("删除",tbv);




    rightMenu->addAction(cutAction);
    rightMenu->addAction(copyAction);
    rightMenu->addAction(pasteAction);
    rightMenu->addAction(deleteAction);

    connect(tbv,SIGNAL(customContextMenuRequested(QPoint)),this,SLOT(clicked_rightMenu(QPoint)));

    connect(deleteAction,SIGNAL(triggered(bool)),this,SLOT(deletefunc()));
}

MainWindow::~MainWindow()
{
    delete ui;
}



void MainWindow::initModel1(QStandardItemModel *model)
{

    QList<Student> students = stuSer.getStudents();
    for(Student stu : students){
       QList<QStandardItem*> items ;
       items << new QStandardItem(stu.getNum())
             << new QStandardItem(stu.getName())
             << new QStandardItem(QString().setNum(stu.getAge()))
             << new QStandardItem(stu.getAddr());
       model->appendRow(items);
    }
}


void MainWindow::deletefunc()
{
//   QMessageBox::information(this,"信息","右键删除");

    QString text = QInputDialog::getText(this,"title","name");

}


/**
 * @brief MainWindow::on_pushButton_clicked
 * 添加人员
 */
void MainWindow::on_pushButton_clicked()
{    
    ad->setWindowTitle("添加人员");
    ad->setModal(true);
    ad->setStu(Student());
    ad->initData();
    ad->show();
}

/**
 * @brief MainWindow::on_pushButton_2_clicked
 * 编辑
 */
void MainWindow::on_pushButton_2_clicked()
{
    QModelIndexList row = tbv->selectionModel()->selectedIndexes();
    if(row.size()>0){
        ad->setWindowTitle("修改人员");
        ad->setModal(true);
        QString num = row[0].data().toString();
        QString name = row[1].data().toString();
        int age = row[2].data().toInt();
        QString addr = row[3].data().toString();
        Student stu(num,name,age,addr);
        ad->setStu(stu);
        ad->initData();
        ad->show();
    }
}

void MainWindow::on_pushButton_3_clicked()
{
    QAbstractItemView::SelectionMode selectModel = tbv->selectionMode();
    if(selectModel == QAbstractItemView::SingleSelection){
        QModelIndexList rows = tbv->selectionModel()->selectedRows();
        if(rows.size()>0){
            QModelIndex mi = rows[0];
            int localRow = mi.row();
            tbv->model()->removeRow(localRow);
        }
    }else if(selectModel == QAbstractItemView::MultiSelection){
        QModelIndexList rows = tbv->selectionModel()->selectedRows();
        QList<QModelIndex>::iterator it = rows.begin();
        for (;it != rows.end();it++) {
            QModelIndex mi = *it;
            int localRow = mi.row();
            tbv->model()->removeRow(localRow);
        }
    }
}



void MainWindow::clicked_rightMenu(const QPoint &pos)
{
    rightMenu->exec(QCursor::pos());
//    rightMenu->exec(pos);


}
